package scripts;

import api.CommonApi;
import api.LoginApi;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import org.uncommons.reportng.HTMLReporter;
import util.AssertUtil;
import util.ResponseData;
import util.TestDataUtil;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Random;
@Listeners({HTMLReporter.class})

public class TestLogin {
    private static final CommonApi common = new CommonApi();
    private static final LoginApi login = new LoginApi();
    private static final Logger logger = LoggerFactory.getLogger(TestLogin.class);

    @DataProvider
    public Object[][] build_data() throws IOException {
//        String file = FileUtils.readFileToString(new File("./data/login.json"), "utf-8");
//        JSONArray jsonArray = JSONObject.parseArray(file);
//        Object[][] test_data = new Object[jsonArray.size()][];
//        for (int i = 0; i < jsonArray.size(); i++) {
//            JSONObject jsonObject = jsonArray.getJSONObject(i);
//            test_data[i] = new Object[]{jsonObject.get("username"), jsonObject.get("password"), jsonObject.get("valicode"),
//                    jsonObject.get("statusCode"), jsonObject.get("body_code"), jsonObject.get("description")};
//
//        }
        String keys="username,password,valicode,statusCode,body_code,description";
        Object[][] test_data = TestDataUtil.build_data("login.json", keys);
        logger.info("test_data={}", Arrays.deepToString(test_data));
        return test_data;
    }
    @Test
    public static void test_01() throws IOException, URISyntaxException {
        ResponseData responseData = login.login("admin","12345678","8888");
        Assert.assertEquals(responseData.getCode(), 200);
        Assert.assertEquals(responseData.getBody().get("status"), 100);
        Assert.assertEquals(responseData.getBody().getString("description"), "验证码错误");
    }
    @Test(dataProvider = "build_data")
    public static void test_02(String username, String password, String code, int statusCode, int body_code, String description) throws IOException, URISyntaxException {
        ResponseData verify = common.get_verify(String.valueOf(new Random().nextDouble()));
        logger.info("username={} password={} code={} statusCode={} status={} ", username, password, code, statusCode, body_code);
        Assert.assertEquals(verify.getCode(), 200);
        ResponseData responseData = login.login(username, password, code);
        AssertUtil.assertCommon(responseData,statusCode,body_code,description);
    }
    @Test
    public static void test_02() throws IOException, URISyntaxException {
        ResponseData responseData = login.login("admin","12345678","8888");
        Assert.assertEquals(responseData.getCode(), 200);
        Assert.assertEquals(responseData.getBody().get("status"), 100);
        Assert.assertEquals(responseData.getBody().getString("description"), "验证码错误");
    }

}
